//
//  SearchViewController.m
//  Final
//
//  Created by Lenrony on 2021/12/2.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SearchViewController.h"
#import "./Views/ArticleView.h"

@interface SearchViewController()

@property(strong, nonatomic) UITextField *searchText;
@property(strong, nonatomic) UIButton *searchButton;
@property(nonatomic) NSInteger recordNum;
@property(strong, nonatomic) NSString *keywords;
@property (strong, nonatomic) UIAlertController* alert;
@property (strong, nonatomic) NSMutableArray *rawUsers;
@property (strong ,nonatomic) NSMutableArray *articles;
@property (strong, nonatomic) ArticleView *artv;

@property(strong, nonatomic) ModelController *mc;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mc = [ModelController getInstance];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"gray.png"]];
    [self setupSearchBar];
    [self setupSearchButton];
    [self setupCollectionView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    self.searchText.text = @"";
//    self.keywords = self.searchText.text;
}

- (void)setupSearchBar {
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 110)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    
    self.searchText = [[UITextField alloc] initWithFrame:CGRectMake(30, 60, self.view.frame.size.width-140, 30)];
    self.searchText.placeholder = @"请输入搜索关键词";
    [self.searchText setBorderStyle:UITextBorderStyleRoundedRect];
    self.searchText.layer.cornerRadius = 5;
    self.searchText.layer.borderWidth = 1;
    [self.searchText addTarget:self action:@selector(textFieldDidChage:) forControlEvents:UIControlEventEditingChanged];
    [topView addSubview:self.searchText];
}

- (void)setupSearchButton {
    _searchButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-100, 60, 70, 30)];
    [_searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [_searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_searchButton setBackgroundColor:[UIColor colorWithRed:50/255.0 green:153/255.0 blue:50/255.0 alpha:1.0]];
    _searchButton.layer.cornerRadius = 5;
    _searchButton.layer.masksToBounds = YES;
    [_searchButton addTarget:self action:@selector(searchArticleClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_searchButton];
}

- (void)setupCollectionView {
    _recordNum = 0;
    self.articles = [[NSMutableArray alloc] init];
    self.rawUsers = [[NSMutableArray alloc] init];
}

- (void)textFieldDidChage:(id)sender {
    UITextField *temp = (id)sender;
    _keywords = temp.text;
}

- (void)searchArticleClick:(id)sender {
    [self.view willRemoveSubview:_artv];
    if (_articles.count != 0) {
        [_articles removeAllObjects];
        [_rawUsers removeAllObjects];
    }
    
    _articles = [[ModelController getInstance] queryArticle:_keywords];
    if (self.articles == nil) {
        [self showAlertWithTitle:@"联网失败" andText:@"请确认网络连接正常并刷新！"];
        return;
    }
    if (self.articles.count == 0) {
        [self showAlertWithTitle:@"当前没有帖子" andText:@"未找到相关帖子..."];
        return;
    }
    
    if ([_keywords isEqualToString:@""]) {
        _articles = [[NSMutableArray alloc] init];
        _rawUsers = [[NSMutableArray alloc] init];
    }
    for (Article *art in self.articles) {
        [self.rawUsers addObject:[[ModelController getInstance] getRawUser:art.u_id]];
    }
    
    self.artv = [[ArticleView alloc] initWithViewController:self andRawUsers:self.rawUsers andArticles:self.articles];
    [self.artv setFrame:CGRectMake(0, 120, self.view.frame.size.width, self.view.frame.size.height-120-[[UIApplication sharedApplication] statusBarFrame].size.height)];
    [self.artv.articlesView setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height-120-[[UIApplication sharedApplication] statusBarFrame].size.height)];
    [self.view addSubview:self.artv];
}

- (void)showAlertWithTitle: (NSString*)title
                   andText: (NSString*)text
{
    self.alert = [UIAlertController alertControllerWithTitle:title message:text preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:self.alert animated:YES completion:nil];
    [self performSelector:@selector(hideAlert) withObject:self.alert afterDelay:1.5];
}

- (void)hideAlert
{
    [self.alert dismissViewControllerAnimated:YES completion:^{
        if(self.mc.cur_user != nil){
            [self viewWillAppear:YES];
        }
    }];
}

@end
